package com.gitee.target123.develop.login;

import com.gitee.target123.develop.login.cache.RedisStateCache;
import com.gitee.target123.develop.login.properties.JustAuthProperties;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;

abstract class JustAuthStateCacheConfiguration {

    /**
     * Redis 缓存
     */
    @ConditionalOnClass(RedisTemplate.class)
    @ConditionalOnMissingBean(AuthStateCache.class)
    @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "redis")
    static class Redis {
        @Bean
        public AuthStateCache authStateCache(RedisTemplate<String, String> redisTemplate, JustAuthProperties justAuthProperties) {
            return new RedisStateCache(redisTemplate, justAuthProperties.getCache());
        }
    }

    /**
     * 默认缓存
     */
    @ConditionalOnMissingBean(AuthStateCache.class)
    @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "default", matchIfMissing = true)
    static class Default {
        @Bean
        public AuthStateCache authStateCache() {
            return AuthDefaultStateCache.INSTANCE;
        }
    }

    /**
     * 自定义缓存
     */
    @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "custom")
    static class Custom {

        @Bean
        @ConditionalOnMissingBean(AuthStateCache.class)
        public AuthStateCache authStateCache() {
            throw new RuntimeException("请自行实现 me.zhyd.oauth.cache.AuthStateCache");
        }
    }
}
